﻿using System;
using System.Collections.Generic;
using System.Web;

using System.Text;
using System.Web.UI;
using VTNT.Framework.Utils;


namespace VTNT.Framework.Web.Global
{
    public class CAppEnvironment
    {
        public void RegisterPortalConfig(Page page)
        {
            CJson config = new CJson("VTNT Configuration", "VTNT", false);
            config.AddVariable("Web_Domain", CUrl.Domain);
            config.AddVariable("Web_HostName", CUrl.Host);

            config.AddVariable("Client_Key", CAppConfig.ClientKey);

            config.AddVariable("Service_Data", CAppConfig.ServiceData);
            config.AddVariable("Template", CAppConfig.Template);
            config.AddVariable("Type", CAppConfig.Type);

            string layout = GetLayoutHtml(CAppConfig.Template);
            if (!String.IsNullOrEmpty(layout))
            {
                config.AddVariable("Layout", layout);
            }

            //CECMAScriptPacker p = new CECMAScriptPacker(CECMAScriptPacker.PackerEncoding.Normal, true, true);
            //string packed = p.Pack(config.ToString()).Replace("\n", "\r\n");
            //page.ClientScript.RegisterClientScriptBlock(page.GetType(), "VTNT_scriptkey", packed, true);

            page.ClientScript.RegisterClientScriptBlock(page.GetType(), "VTNT_scriptkey", config.ToString(), true);
        }
        private string GetLayoutHtml(string template)
        {
            string path = System.AppDomain.CurrentDomain.BaseDirectory + "\\asset\\template\\";
            string layout = CFile.Read(path + template + "\\view\\layout.htm");
            if (!String.IsNullOrEmpty(layout))
            {
                layout = HttpUtility.HtmlEncode(layout.Replace("\r\n", ""));
            }
            return layout;
        }
    }
}